//logs.js
const util = require('../../utils/util.js')

Page({
  data: {
    data : {
      letters: ['A', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'Q', 'R', 'S', 'T', 'W', 'X', 'Y', 'Z'],
      ischecked : true,
      clickLetter: "clickLetter",
      firstLetter:'A',
      id : ''
    },
    brands: {},
    series : {},
    years : {},
    http: util.http+'api/v1/',
    seriesHidden : true,
    yearsHidden: true
  },
  onLoad : function(){
    this.init();
  },
  init : function(){
    var that = this;
    wx.request({
      url: that.data.http + 'brands/',
      data: {
        first_letter: that.data.data.firstLetter
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.branchInt(res.data.results)
      }
    })
  },
  branchInt: function (data) {
    this.setData({
      brands: {
        brandId : '', // 增加了一个识别符，用来设置高点的brand
        brands : data
      }
    })
  },
  clickLetter : function(e){
    var id = e.target.dataset.id;
    var letter = e.target.dataset.letter;
    var that = this;
    this.setData({
      'data.id' : id,
      'data.firstLetter' : letter,
      seriesHidden: true,
      yearsHidden: true

    })
    wx.request({
      url: that.data.http + 'brands/',
      data: {
        first_letter: that.data.data.firstLetter
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.branchInt(res.data.results)
      }
    })
  },
  clickBrand : function(e){
    var id = e.currentTarget.dataset.id;
    var brand = e.currentTarget.dataset.brand;
    this.setData({
      "brands.brandId" : id,
      brandName : brand,
      seriesHidden : false,
      yearsHidden: true
    })
    var that = this;
    wx.request({
      url: that.data.http + 'series/',
      data: {
        brand: that.data.brandName
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.serieInt(res.data.results)
      }
    })
  },
  serieInt : function(data){
    this.setData({
      series: {
        serieId: 0, // 增加了一个识别符，用来设置高点的车系
        series: data
      }
    })
  },
  clickSerie : function(e){
    var id = e.currentTarget.dataset.id;
    var serie = e.currentTarget.dataset.serie;
    this.setData({
      "series.serieId": id,
      seriesName: serie,
      yearsHidden: false
    })
    var that = this;
    wx.request({
      url: that.data.http + 'cargos/',
      data: {
        series: that.data.seriesName
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var yearTime = res.data.results;
        var filterYear = {},
            length = yearTime.length;
        for (var i = 0; i < length;i++){
          for (var j = length-1; j>=0 ; j--){
            if (yearTime[i].get_year == yearTime[j].get_year){
              filterYear[yearTime[i].get_year] = yearTime[i].get_year
            }
          }
        }
        that.yearInt(filterYear)
      }
    })
  },
  yearInt : function(data){
    this.setData({
      years: {
        yearId: 0, // 增加了一个识别符，用来设置高点的年份
        years: data
      }
    })
  },
  clickYear : function(e){
    var id = e.currentTarget.dataset.id;
    this.setData({
      "years.yearId": id
    })
  }
})
